package com.whpu.mall.product.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.whpu.common.utils.PageUtils;
import com.whpu.common.utils.Query;
import com.whpu.mall.product.dao.BrandDao;
import com.whpu.mall.product.dao.CategoryBrandRelationDao;
import com.whpu.mall.product.dao.CategoryDao;
import com.whpu.mall.product.entity.BrandEntity;
import com.whpu.mall.product.entity.CategoryBrandRelationEntity;
import com.whpu.mall.product.entity.CategoryEntity;
import com.whpu.mall.product.service.BrandService;
import com.whpu.mall.product.service.CategoryBrandRelationService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;


@Service("categoryBrandRelationService")
public class CategoryBrandRelationServiceImpl extends ServiceImpl<CategoryBrandRelationDao, CategoryBrandRelationEntity> implements CategoryBrandRelationService {

    @Autowired
    private BrandDao brandDao;

    @Autowired
    private CategoryDao categoryDao;

    @Autowired
    private CategoryBrandRelationDao relationDao;

    @Autowired
    private BrandService brandService;

    @Override
    public PageUtils queryPage(Map<String, Object> params) {
        IPage<CategoryBrandRelationEntity> page = this.page(
                new Query<CategoryBrandRelationEntity>().getPage(params),
                new QueryWrapper<CategoryBrandRelationEntity>()
        );

        return new PageUtils(page);
    }

    @Override
    @Transactional
    public void saveDetail(CategoryBrandRelationEntity categoryBrandRelation) {
        //查询id
        Long brandId = categoryBrandRelation.getBrandId();
        Long catelogId = categoryBrandRelation.getCatelogId();
        //调用对应mapper查询对应数据
        BrandEntity brandEntity = brandDao.selectById(brandId);
        CategoryEntity categoryEntity = categoryDao.selectById(catelogId);
        //设置到pms_category_brand_relation表中
        categoryBrandRelation.setBrandName(brandEntity.getName());
        categoryBrandRelation.setCatelogName(categoryEntity.getName());
        //保存
        this.save(categoryBrandRelation);

    }

    /**
     * 同步更新其他关联表中的数据,根据id更改name
     * @param brandId
     * @param name
     */
    @Override
    @Transactional
    public void updateBrand(Long brandId, String name) {
        CategoryBrandRelationEntity relationEntity = new CategoryBrandRelationEntity();
        relationEntity.setBrandName(name);
        relationEntity.setBrandId(brandId);
        this.update(relationEntity,new UpdateWrapper<CategoryBrandRelationEntity>().eq("brand_id",brandId));
    }

    /**
     * 级联更新分类所有关联数据
     * @param catId
     * @param name
     */
    @Override
    public void updateCategory(Long catId, String name) {
        baseMapper.updateCategory(catId,name);
    }

    /**
     * 根据分类id查对应关联品牌
     * @param catId
     * @return
     */
    @Override
    public List<BrandEntity> getBrandListByCatId(Long catId) {

        List<CategoryBrandRelationEntity> catelogList = relationDao
                .selectList(new QueryWrapper<CategoryBrandRelationEntity>()
                        .eq("catelog_id", catId));

        //获取更多品牌信息,方便代码复用
        List<BrandEntity> BrandEntityList = catelogList.stream().map(item -> {
            return brandService.getById(item.getBrandId());
        }).collect(Collectors.toList());

        return BrandEntityList;
    }

}
